package index

import (
	"fmt"
	"github.com/wycto/weigo"
	"time"
)

type TestController struct {
	weigo.Controller
}

func (receiver *TestController) Index() {
	s := []int{7, 2, 8, -9, 4, 0}

	c := make(chan int)
	go sum(s[:len(s)/2], c)
	go sum(s[len(s)/2:], c)
	x, y := <-c, <-c // 从通道 c 中接收

	fmt.Println(x, y, x+y)
}

func sum(s []int, c chan int) {
	sum := 0
	for _, v := range s {
		sum += v
	}
	c <- sum // 把 sum 发送到通道 c
}

func (receiver *TestController) Route() {
	fmt.Println("param", receiver.Context.Param)
	fmt.Println("Get", receiver.Context.Get)
	fmt.Println("Post", receiver.Context.Post)
	fmt.Println("---------")
}

func (receiver *TestController) Time() {
	datetime := "20210416184859" //待转化为时间戳的字符串

	//日期转化为时间戳
	timeLayout := "20060102150405"       //转化所需模板
	loc, _ := time.LoadLocation("Local") //获取时区
	tmp, _ := time.ParseInLocation(timeLayout, datetime, loc)
	timestamp := tmp.Unix() //转化为时间戳 类型是int64
	fmt.Println(timestamp)

	//时间戳转化为日期
	datetime = time.Unix(timestamp, 0).Format("2006-01-02 15:04:05")
	fmt.Println(datetime)
}
